﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using C.Entities;
using C.Unite;
using ChanExt;
using System.Text;

namespace ArtLand.OA.Project
{
    public partial class Main : CommonPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Load_View();
                ArtGridBind();
            }
        }

        protected void Load_View()
        {
            Sys_DeskTop desk = TbsDbSession.Default.From<Sys_DeskTop>().Where(Sys_DeskTop._.Uid == CookInfo.EAdminid()).ToFirst();
            if (desk != null)
            {
                if (desk.DeskJson.Contains("a1"))
                    chkList.Items[0].Selected = true;
                if (desk.DeskJson.Contains("a2"))
                    chkList.Items[1].Selected = true;
                if (desk.DeskJson.Contains("b1"))
                    chkList.Items[2].Selected = true;
                if (desk.DeskJson.Contains("b2"))
                    chkList.Items[3].Selected = true;
                if (desk.DeskJson.Contains("c1"))
                    chkList.Items[4].Selected = true;
                // if (desk.DeskJson != "[]")
                //Alert.Show(JsonMapper.ToObject(desk.DeskJson)[0].ToString());
            }
            if (desk != null)
            {
                 hidJson.Text = desk.DeskJson;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Sys_DeskTop deskTop = new Sys_DeskTop();
            Sys_DeskTop desk = TbsDbSession.Default.From<Sys_DeskTop>().Where(Sys_DeskTop._.Uid == CookInfo.EAdminid()).ToFirst();
            if (desk != null)
            {
                deskTop.ID = desk.ID;
                deskTop.Attach();
                deskTop.DeskJson = hidJson.Text;
                TbsDbSession.Default.Update<Sys_DeskTop>(deskTop);
            }
            else
            {
                deskTop.DeAttach();
                deskTop.Uid = CookInfo.EAdminid();
                deskTop.DeskJson = hidJson.Text;
                TbsDbSession.Default.Insert<Sys_DeskTop>(deskTop);
            }
            //Alert.Show(hidJson.Text);
        }


        protected void ArtGridBind()
        {
            List<Proj_Article> artlist = TbsDbSession.Default.From<Proj_Article>().OrderBy(Proj_Article._.AddTime.Desc).Top(10).ToList();
            grid1.DataSource = artlist;
            grid1.DataBind();
        }

        protected void grid1_RowDataBound(object sender, GridRowEventArgs e)
        {
            string color = e.Values[3].ToString();
            string isbold = e.Values[4].ToString();
            string title = e.Values[1].ToString();
            string bold = "";
            if (isbold == "True")
                bold = "bold";
            title = string.Format("<span style=\"color:{0};font-weight:{1};\">" + title + "</span>", color, bold);
            int id = int.Parse(e.Values[2].ToString());
            string noread = "<img src=\"/icon/sms-unread.gif\" />";
            string read = "<img src=\"/icon/sms-readed.gif\" />";
            Proj_ArtReadLogs pa = TbsDbSession.Default.From<Proj_ArtReadLogs>().Where(Proj_ArtReadLogs._.ArtID == id && Proj_ArtReadLogs._.Uid == CookInfo.EAdminid()).ToFirst();
            if (pa != null)
                e.Values[0] = read;
            else
                e.Values[0] = noread;
            e.Values[1] = title;
        }

        protected void btn_a3_Click(object sender, EventArgs e)
        {
            ArtGridBind();
        }

        protected void grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
        {
            int[] list = grid1.SelectedRowIndexArray;
            int index = list[0];
            int id = int.Parse(grid1.DataKeys[index][0].ToString());
            int c = TbsDbSession.Default.From<Proj_ArtReadLogs>().Where(Proj_ArtReadLogs._.ArtID == id && Proj_ArtReadLogs._.Uid == CookInfo.EAdminid()).Count();
            if (c == 0)
            {
                Proj_ArtReadLogs read = new Proj_ArtReadLogs();
                read.DeAttach();
                read.AddTime = DateTime.Now;
                read.ArtID = id;
                read.Mtype = 0;
                read.Uid = CookInfo.EAdminid();
                TbsDbSession.Default.Insert<Proj_ArtReadLogs>(read);
            }
            FormInfo.GridView(grid1, "/PublicModel/ArtView.aspx", "id", "", "查看公告或信息");
            if (c == 0)
                ArtGridBind();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            PageContext.RegisterStartupScript(Window4.GetShowReference());
        }
        protected void btnDeskSave_Click(object sender, EventArgs e)
        {
            string[] arr = chkList.SelectedValueArray;
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            for (int i = 0; i < arr.Length; i++)
            {
                List<string> ls = StringHelper.GetSubStringList(arr[i], ';');
                sb.Append("{\"id\":\"" + ls[0] + "\",\"col\":" + ls[1] + ",\"row\":" + ls[2] + "},");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.Append("]");
            Sys_DeskTop deskTop = new Sys_DeskTop();
            Sys_DeskTop desk = TbsDbSession.Default.From<Sys_DeskTop>().Where(Sys_DeskTop._.Uid == CookInfo.EAdminid()).ToFirst();
            if (desk != null)
            {
                deskTop.ID = desk.ID;
                deskTop.Attach();
                deskTop.DeskJson = sb.ToString();
                TbsDbSession.Default.Update<Sys_DeskTop>(deskTop);
            }
            else
            {
                deskTop.DeAttach();
                deskTop.Uid = CookInfo.EAdminid();
                deskTop.DeskJson = sb.ToString();
                TbsDbSession.Default.Insert<Sys_DeskTop>(deskTop);
            }
            //Tab2.IFrameUrl = "/Main.aspx";
            PageContext.Refresh();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
           
            Sys_DeskTop desk = TbsDbSession.Default.From<Sys_DeskTop>().Where(Sys_DeskTop._.Uid == CookInfo.EAdminid()).ToFirst();
            if (desk != null)
            {
                if (desk.DeskJson.Contains("a1"))
                    chkList.Items[0].Selected = true;
                else
                    chkList.Items[0].Selected = false;

                if (desk.DeskJson.Contains("a2"))
                    chkList.Items[1].Selected = true;
                else
                    chkList.Items[1].Selected = false;

                if (desk.DeskJson.Contains("a3"))
                    chkList.Items[2].Selected = true;
                else
                    chkList.Items[2].Selected = false;

                if (desk.DeskJson.Contains("b1"))
                    chkList.Items[3].Selected = true;
                else
                    chkList.Items[3].Selected = false;

                if (desk.DeskJson.Contains("b2"))
                    chkList.Items[4].Selected = true;
                else
                    chkList.Items[4].Selected = false;
                // if (desk.DeskJson != "[]")
                //Alert.Show(JsonMapper.ToObject(desk.DeskJson)[0].ToString());
            }
            if (desk != null)
            {
                hidJson.Text = desk.DeskJson;
            }
            //Load_View();
        }
    }
}